var Page = function(){
    this.init();
};
Page.prototype = {
    params: {
        passport: '',
        gmtStart:'',
        gmtEnd:'',
        salegmtStart:'',
        salegmtEnd:'',
        thisMonthBorrow:null,
        lastMonthBorrow:null,
        thisWeekBorrow:null,
        lastWeekBorrow:null,

        thisMonthReturn:null,
        lastMonthReturn:null,
        thisWeekReturn:null,
        lastWeekReturn:null
    },
    init: function(){
        this.initParams();
        this.initEvent();
    },
    initParams: function(){
        this.params.passport = Pub.getCookie('_PASSPORT');
        this.params.gmtStart = Pub._showDay(2).getWeekStartDate+' 00:00:00';
        this.params.gmtEnd = Pub._showDay(2).getWeekEndDate+' 23:59:59';
        this.params.salegmtStart = Pub._showDay(2).getWeekStartDate+' 00:00:00';
        this.params.salegmtEnd = Pub._showDay(2).getWeekEndDate+' 23:59:59';
    },
    getData: function(){
        var _this = this;
        var paramsObj = {
            passport: _this.params.passport
        };
        Api.systemhomepage(paramsObj, function(data){
            console.log(data);
            var data=data.list;
            $('#userCount').text(data.userCount);
            $('#activeUser').text(data.activeUser);
            $('#collectionToday').text('￥'+data.collectionToday);
            $('#todayNewUser').text(data.todayNewUser);
            $('#sumCollection').text('￥'+data.sumCollection);

            $('#totalBorrowingsTodayNum').text(data.totalBorrowingsTodayNum);
            $('#totalBorrowings').text('￥'+data.totalBorrowings);
            $('#yesterdayWeekBorrowings ').text('￥'+data.yesterdayWeekBorrowings );
            $('#nearlyWeekBorrowings').text('￥'+data.nearlyWeekBorrowings);
            $('#thisMonthBorrowings').text('￥'+data.thisMonthBorrowings);
            $('#auditNum').text(data.auditNum);

        });
    },
    thisMonthBorrow:function(){
        var _this = this;
        var paramsObj = {
            passport: _this.params.passport,
            gmtStart: Pub._showDay(3).getMonthStartDate+' 00:00:00',
            gmtEnd: Pub._showDay(3).getMonthEndDate+' 23:59:59'
        };
        Api.borrowingstatistics(paramsObj, function(data){
            console.log(data);
            // 本月订单数量
            _this.params.thisMonthBorrow=_this.sum(data.arrNum);
            $('#thisMonth').text(_this.params.thisMonthBorrow);
            var Obj = {
                passport: _this.params.passport,
                gmtStart: Pub._showDay(3).getLastMonthStartDate+' 00:00:00',
                gmtEnd: Pub._showDay(3).getLastMonthEndDate+' 23:59:59'
            };
            Api.borrowingstatistics(Obj, function(data){
                console.log(data);
                // 上月月订单数量
                _this.params.lastMonthBorrow=_this.sum(data.arrNum);
                var than=((_this.params.thisMonthBorrow-_this.params.lastMonthBorrow)/_this.params.lastMonthBorrow);

                if (than==Infinity) {
                    $('#contrastMonth').text('100%').removeClass('c-red').addClass('c-green');
                    $('#contrastMonthImg').attr('src', 'images/green-arrow.png');
                }else if (than==-Infinity){
                    $('#contrastMonth').text('100%').removeClass('c-green').addClass('c-red');
                    $('#contrastMonthImg').attr('src', 'images/red-arrow.png');
                }else if (than>0) {
                    $('#contrastMonth').text((than.toFixed(2)*100)+'%').removeClass('c-red').addClass('c-green');
                    $('#contrastMonthImg').attr('src', 'images/green-arrow.png');
                }else if (than<0) {
                    $('#contrastMonth').text((than.toFixed(2)*100)+'%').removeClass('c-green').addClass('c-red');
                    $('#contrastMonthImg').attr('src', 'images/red-arrow.png');
                };
            });
        });
    },
    thisWeekBorrow:function(){
        var _this = this;
        var paramsObj = {
            passport: _this.params.passport,
            gmtStart: Pub._showDay(2).getWeekStartDate+' 00:00:00',
            gmtEnd: Pub._showDay(2).getWeekEndDate+' 23:59:59'
        };
        Api.borrowingstatistics(paramsObj, function(data){
            //本周订单数量
            _this.params.thisWeekBorrow=_this.sum(data.arrNum);
            $('#thisWeek').text(_this.params.thisWeekBorrow);
            var Obj = {
                passport: _this.params.passport,
                gmtStart: Pub._showDay(2).getLastWeekStartDate+' 00:00:00',
                gmtEnd: Pub._showDay(2).getLastWeekEndDate+' 23:59:59'
            };
            Api.borrowingstatistics(Obj, function(data){
                //上周订单数量
                var lastWeekOrder=_this.sum(data.arrNum);
                var than=((_this.params.thisWeekBorrow-lastWeekOrder)/lastWeekOrder);

                if (than==Infinity) {
                    $('#contrastWeek').text('100%').removeClass('c-red').addClass('c-green');
                    $('#contrastWeekImg').attr('src', 'images/green-arrow.png');
                }else if (than==-Infinity){
                    $('#contrastWeek').text('100%').removeClass('c-green').addClass('c-red');
                    $('#contrastWeekImg').attr('src', 'images/red-arrow.png');
                }else if (than>0) {
                    $('#contrastWeek').text((than.toFixed(2)*100)+'%').removeClass('c-red').addClass('c-green');
                    $('#contrastWeekImg').attr('src', 'images/green-arrow.png');
                }else if (than<0) {
                    $('#contrastWeek').text((than.toFixed(2)*100)+'%').removeClass('c-green').addClass('c-red');
                    $('#contrastWeekImg').attr('src', 'images/red-arrow.png');
                };
            });
        });
    },
    borrowEcharts:function(){
        var _this = this;
        var paramsObj = {
            passport: _this.params.passport,
            gmtStart: _this.params.gmtStart,
            gmtEnd: _this.params.gmtEnd
        };
        Api.borrowingstatistics(paramsObj, function(data){
            console.log(data);
            var weekArr=[];
            var week=['周一','周二','周三','周四','周五','周六','周日']
            if (data.arrTime.length==7) {
                for (var i = 0; i < data.arrTime.length; i++) {
                    weekArr.push(data.arrTime[i]+week[i]);
                };
                data.arrTime=weekArr;
            };
            //基于准备好的dom，初始化echarts实例
            var myChartOrder = echarts.init(document.getElementById('order-statistics'));

            // 指定图表的配置项和数据
            optionOrder = {
                tooltip : {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'cross',
                        label: {
                            backgroundColor: '#6a7985',
                        }
                    }
                },
                legend: {
                    data:['借贷']
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                xAxis : [
                    {
                        type : 'category',
                        boundaryGap : false,
                        data : data.arrTime,

                    }
                ],
                yAxis : [
                    {
                        type : 'value'
                    }
                ],
                series : [
                    {

                        type:'line',
                        stack: '借贷',
                        smooth:true,
                        areaStyle: {normal: {color:'#87CEFA'}},
                        itemStyle:{
                            normal:{
                                lineStyle:{
                                    color:'#7EC0EE'
                                }
                            }
                        },
                        data:data.arrNum
                    }
                ]
            };
            // 使用刚指定的配置项和数据显示图表。
            myChartOrder.setOption(optionOrder);
        });
    },
    ReturnEcharts:function(){
        var _this = this;
        var paramsObj = {
            passport: _this.params.passport,
            gmtStart: _this.params.salegmtStart,
            gmtEnd: _this.params.salegmtEnd
        };
        Api.reimbursementofstatistical(paramsObj, function(data){

            // $('#thisMonthSale').text(data.thisMonth);
            // $('#thisWeekSale').text(data.thisWeek);
            // var contrastWeek=data.contrastWeek+'';
            // var contrastMonth=data.contrastMonth+'';
            // if (contrastWeek.indexOf('-')!==-1) {
            //     $('#contrastWeekSale').text(data.contrastWeek+'%').removeClass('c-green').addClass('c-red');
            // }else{
            //     $('#contrastWeekSale').text(data.contrastWeek+'%');
            // };
            // if (contrastMonth.indexOf('-')!==-1) {
            //     $('#contrastMonthSale').text(data.contrastMonth+'%').removeClass('c-green').addClass('c-red');
            // }else{
            //     $('#contrastMonthSale').text(data.contrastMonth+'%');
            // };
            var weekArr=[];
            var week=['周一','周二','周三','周四','周五','周六','周日']
            if (data.arrTime.length==7) {
                for (var i = 0; i < data.arrTime.length; i++) {
                    weekArr.push(data.arrTime[i]+week[i]);
                };
                data.arrTime=weekArr;
            };
            //基于准备好的dom，初始化echarts实例
            var myChartSales = echarts.init(document.getElementById('sale-statistics'));

            // 指定图表的配置项和数据
            optionSales = {
                tooltip : {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'cross',
                        label: {
                            backgroundColor: '#6a7985',
                        }
                    }
                },
                legend: {
                    data:['还款']
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                xAxis : [
                    {
                        type : 'category',
                        boundaryGap : false,
                        data : data.arrTime
                    }
                ],
                yAxis : [
                    {
                        type : 'value'
                    }
                ],
                series : [
                    {

                        type:'line',
                        stack: '还款',
                        smooth:true,
                        areaStyle: {normal: {color:'#87CEFA'}},
                        itemStyle:{
                            normal:{
                                lineStyle:{
                                    color:'#7EC0EE'
                                }
                            }
                        },
                        data:data.arrNum
                    }
                ]
            };
            // 使用刚指定的配置项和数据显示图表。
            myChartSales.setOption(optionSales);
        });
    },
    thisMonthReturn:function(){
        var _this = this;
        var paramsObj = {
            passport: _this.params.passport,
            gmtStart: Pub._showDay(3).getMonthStartDate+' 00:00:00',
            gmtEnd: Pub._showDay(3).getMonthEndDate+' 23:59:59'
        };
        Api.reimbursementofstatistical(paramsObj, function(data){
            console.log(data);
            //本月销售
            _this.params.thisMonthReturn=_this.sum(data.arrNum);
            $('#thisMonthSale').text(_this.params.thisMonthReturn);
            var Obj = {
                passport: _this.params.passport,
                gmtStart: Pub._showDay(3).getLastMonthStartDate+' 00:00:00',
                gmtEnd: Pub._showDay(3).getLastMonthEndDate+' 23:59:59'
            };
            Api.reimbursementofstatistical(Obj, function(data){
                // 上月销售
                _this.params.lastMonthReturn=_this.sum(data.arrNum);
                var than=((_this.params.thisMonthReturn-_this.params.lastMonthReturn)/_this.params.lastMonthReturn);
                if (than==Infinity) {
                    $('#contrastMonthSale').text('100%').removeClass('c-red').addClass('c-green');
                    $('#thisMonthSaleimg').attr('src', 'images/green-arrow.png');
                }else if (than==-Infinity){
                    $('#contrastMonthSale').text('100%').removeClass('c-green').addClass('c-red');
                    $('#thisMonthSaleimg').attr('src', 'images/red-arrow.png');
                }else if (than>0) {
                    $('#contrastMonthSale').text((than.toFixed(2)*100)+'%').removeClass('c-red').addClass('c-green');
                    $('#thisMonthSaleimg').attr('src', 'images/green-arrow.png');
                }else if (than<0) {
                    $('#contrastMonthSale').text((than.toFixed(2)*100)+'%').removeClass('c-green').addClass('c-red');
                    $('#thisMonthSaleimg').attr('src', 'images/red-arrow.png');
                };
            });
        });
    },
    thisWeekReturn:function(){
        var _this = this;
        var paramsObj = {
            passport: _this.params.passport,
            gmtStart: Pub._showDay(2).getWeekStartDate+' 00:00:00',
            gmtEnd: Pub._showDay(2).getWeekEndDate+' 23:59:59'
        };
        Api.reimbursementofstatistical(paramsObj, function(data){
            _this.params.thisWeekReturn=_this.sum(data.arrNum);
            $('#thisWeekSale').text(_this.params.thisWeekReturn);
            var Obj = {
                passport: _this.params.passport,
                gmtStart: Pub._showDay(2).getLastWeekStartDate+' 00:00:00',
                gmtEnd: Pub._showDay(2).getLastWeekEndDate+' 23:59:59'
            };
            console.log(Obj);
            Api.reimbursementofstatistical(Obj, function(data){
                var lastWeekReturn=_this.sum(data.arrNum);
                var than=((_this.params.thisMonthBorrow-lastWeekReturn)/lastWeekReturn);
                if (than==Infinity) {
                    $('#contrastWeekSale').text('100%').removeClass('c-red').addClass('c-green');
                    $('#thisWeekSaleimg').attr('src', 'images/green-arrow.png');
                }else if (than==-Infinity){
                    $('#contrastWeekSale').text('100%').removeClass('c-green').addClass('c-red');
                    $('#thisWeekSaleimg').attr('src', 'images/red-arrow.png');
                }else if (than>0) {
                    $('#contrastWeekSale').text((than.toFixed(2)*100)+'%').removeClass('c-red').addClass('c-green');
                    $('#thisWeekSaleimg').attr('src', 'images/green-arrow.png');
                }else if (than<0) {
                    $('#contrastWeekSale').text((than.toFixed(2)*100)+'%').removeClass('c-green').addClass('c-red');
                    $('#thisWeekSaleimg').attr('src', 'images/red-arrow.png');
                };
            });
        });
    },
    sum:function(arr){
        var sum = 0;
        for(var i=0;i<arr.length;i++){
            sum += arr[i];
        }
        return sum;
    },
    other:function(){
        var _this=this;
        $('#borrowTitleBox .r').click(function(event) {
            var type=$(this).attr('dataType');
            $(this).addClass('active');
            $(this).siblings().removeClass('active');
            if (type=='today') {
                _this.params.gmtStart = Pub._showDay(1).today+' 00:00:00';
                _this.params.gmtEnd = Pub._showDay(1).today+' 23:59:59';
            }else if(type=='thisWeek'){
                _this.params.gmtStart = Pub._showDay(2).getWeekStartDate+' 00:00:00';
                _this.params.gmtEnd = Pub._showDay(2).getWeekEndDate+' 23:59:59';
            }else if(type=='thisMonth'){
                _this.params.gmtStart = Pub._showDay(3).getMonthStartDate+' 00:00:00';
                _this.params.gmtEnd = Pub._showDay(3).getMonthEndDate+' 23:59:59';
            };
            _this.borrowEcharts();
        });
        layui.use('laydate', function() {
            var laydate = layui.laydate;
            laydate.render({
                elem: '#borrowTime',
                range: '~',
                done: function(value, date, endDate) {
                    console.log(date);
                    if (date.year) {
                        if (date.month < 10) {
                            date.month = "0" + date.month;
                        }
                        if (date.date < 10) {
                            date.date = "0" + date.date;
                        }
                        if (endDate.month < 10) {
                            endDate.month = "0" + endDate.month;
                        }
                        if (endDate.date < 10) {
                            endDate.date = "0" + endDate.date;
                        }
                        var gmtStart = date.year + '-' + date.month + '-' + date.date + ' 00:00:00';
                        var gmtEnd = endDate.year + '-' + endDate.month + '-' + endDate.date + ' 23:59:59';
                        _this.params.gmtStart = gmtStart;
                        _this.params.gmtEnd = gmtEnd;
                        _this.borrowEcharts();
                    } else {
                        _this.params.gmtStart = '';
                        _this.params.gmtEnd = '';
                    };
                }
            });
        });
        $('#salesTitleBox .r').click(function(event) {
            var type=$(this).attr('dataType');
            $(this).addClass('active');
            $(this).siblings().removeClass('active');
            if (type=='today') {
                _this.params.salegmtStart = Pub._showDay(1).today+' 00:00:00';
                _this.params.salegmtEnd = Pub._showDay(1).today+' 23:59:59';
            }else if(type=='thisWeek'){
                _this.params.salegmtStart = Pub._showDay(2).getWeekStartDate+' 00:00:00';
                _this.params.salegmtEnd = Pub._showDay(2).getWeekEndDate+' 23:59:59';
            }else if(type=='thisMonth'){
                _this.params.salegmtStart = Pub._showDay(3).getMonthStartDate+' 00:00:00';
                _this.params.salegmtEnd = Pub._showDay(3).getMonthEndDate+' 23:59:59';
            };
            _this.ReturnEcharts();
        });
        layui.use('laydate', function() {
            var laydate = layui.laydate;
            laydate.render({
                elem: '#returnTime',
                range: '~',
                done: function(value, date, endDate) {
                    console.log(date);
                    if (date.year) {
                        if (date.month < 10) {
                            date.month = "0" + date.month;
                        }
                        if (date.date < 10) {
                            date.date = "0" + date.date;
                        }
                        if (endDate.month < 10) {
                            endDate.month = "0" + endDate.month;
                        }
                        if (endDate.date < 10) {
                            endDate.date = "0" + endDate.date;
                        }
                        var gmtStart = date.year + '-' + date.month + '-' + date.date + ' 00:00:00';
                        var gmtEnd = endDate.year + '-' + endDate.month + '-' + endDate.date + ' 23:59:59';
                        _this.params.salegmtStart = gmtStart;
                        _this.params.salegmtEnd = gmtEnd;
                        _this.ReturnEcharts();
                    } else {
                        _this.params.salegmtStart = '';
                        _this.params.salegmtEnd = '';
                    };
                }
            });
        });
    },
    initEvent: function(){
        var _this = this;
        // _this.getData();
        //  _this.borrowEcharts();
        //  _this.ReturnEcharts();
        // _this.other();
        // _this.thisMonthBorrow();
        // _this.thisWeekBorrow();
        // _this.thisMonthReturn();
        // _this.thisWeekReturn();

    }
};
var page = new Page();